﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WorkflowActions;

namespace CopyListItemToSecuredList
{
	public partial class Activity1: SequenceActivity
	{
        private EventLog _eventLog;
        private SPList sourceList;

        public static DependencyProperty ListIDProperty = DependencyProperty.Register("ListID", typeof(string), typeof(Activity1));
        public static DependencyProperty ListItemProperty = DependencyProperty.Register("ListItem", typeof(Int32), typeof(Activity1));
        public static DependencyProperty ToListIdProperty = DependencyProperty.Register("ToListId", typeof(string), typeof(Activity1));
        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(Activity1)); 
        #region Public Properties
        [ValidationOption(ValidationOption.Required)]

        [Browsable(true)]

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 
        public string ListID
        {
            get
            {
                return (string)base.GetValue(ListIDProperty);
            }
            set
            {
                base.SetValue(ListIDProperty, value);
            }
        }

        [ValidationOption(ValidationOption.Required)]

        [Browsable(true)]

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 
        public Int32 ListItem
        {
            get
            {
                return (Int32)base.GetValue(ListItemProperty);
            }
            set
            {
                base.SetValue(ListItemProperty, value);
            }
        }
        [ValidationOption(ValidationOption.Required)]

        [Browsable(true)]

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 
        public string ToListId
        {
            get
            {
                return (string)base.GetValue(ToListIdProperty);
            }
            set
            {
                base.SetValue(ToListIdProperty, value);
            }
        }
        [Description("Tool to copy items to a secure list.")]

        [Category("Copy Items.")]

        [ValidationOption(ValidationOption.Required)]

        [Browsable(true)]

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 
        public WorkflowContext __Context
        {

            get { return ((WorkflowContext)(base.GetValue(Activity1.__ContextProperty))); }

            set { base.SetValue(Activity1.__ContextProperty, value); }

        } 
        #endregion 


        //protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        //{
        //    //Set up the Event Logging object 
        //    _eventLog = new EventLog("Workflow");
        //    _eventLog.Source = "SharePoint Workflow";

        //    try
        //    {
        //        //Send the email 
        //        //_eventLog.WriteEntry("Started the workflow");
        //        //Copy();

        //    }
        //    catch(Exception ex)
        //    {
        //        string x = ex.Message;
        //        //_eventLog.WriteEntry("It Broke!");
        //    }
        //    finally
        //    {
        //        //Dispose of the Event Logging object 
        //        //_eventLog.Dispose();
        //    }

        //    //Indicate the activity has closed 
        //    return ActivityExecutionStatus.Closed;
        //}

        private void Copy()
        {
            
            

                
            

           
        } 
        public Activity1()
		{
			InitializeComponent();
		}

        private void _mainCodeActivity_ExecuteCode(object sender, EventArgs e)
        {
            //Raise user privileges
            SPUserToken sysToken = __Context.Site.SystemAccount.UserToken;
            using (var systemSite = new SPSite(__Context.Site.ID, sysToken))
            {
                using (var sysWeb = systemSite.OpenWeb(__Context.Web.ID))
                {
                    
                    //source list

                    try
                    {
                        sourceList = __Context.Web.Lists[new Guid(ListID)];
                    }
                    catch (Exception ex)
                    {

                        string x = ex.Message;
                    }

                    //get list item
                    SPListItem item = __Context.GetListItem(sourceList, ListItem);
                    //destination list
                    SPList destList = __Context.Web.Lists[new Guid(ToListId)];
                    SPListItem targetItem = destList.Items.Add();
                    //copy fields
                    foreach (SPField f in sourceList.Fields)
                    {
                        if (!f.ReadOnlyField && f.InternalName != "Attachments")
                        {
                            targetItem[f.InternalName] = item[f.InternalName];
                        }
                    }

                    //copy attachments
                    foreach (string filename in item.Attachments)
                    {
                        SPFile file = sourceList.Lists[ListItem].ParentWeb.GetFile(item.Attachments.UrlPrefix + filename);
                        byte[] imageData = file.OpenBinary();
                        targetItem.Attachments.Add(filename, imageData);
                    }
                    targetItem.Update();
                }
            }
        }
	}
}
